package cn.itcast.mq.listener;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

/**
 * @author yangjy
 * @version 1.0
 * @description: 监听消息
 * @date 2022/9/8 10:10
 */
@Component
public class SpringRabbitListener {
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage1(String msg) throws InterruptedException{
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage2(String msg) throws InterruptedException{
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }


    /**
     * 广播模式
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }


    /**
     * Direct 直接 模式
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue1"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","blue"}
            )
    )
    public void listenerDirectQueue1(String msg){
        System.err.println("消费者接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue2"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","yellow"}
            )
    )
    public void listenerDirectQueue2(String msg){
        System.out.println("消费者接收到direct.queue2的消息：【" + msg + "】");
    }


    /**
     * topic
     *  通配符规则：
     * #：匹配一个或多个词
     * *：匹配不多不少恰好1个词
     *
     * 举例：
     * item.#：能够匹配item.spu.insert 或者 item.spu
     * item.*：只能匹配item.spu
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = "china.#"
            )
    )
    public void listenerTopicQueue1(String  msg){
        System.err.println("消费者接收到topic.queue1的消息：【" + msg + "】");
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = "#.news"
            )
    )
    public void listenerTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息：【" + msg + "】");
    }




}
